<%@ page contentType="text/html;charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
	<head>
		<title>宿舍列表</title>
		<link rel="stylesheet"
			href="<%=request.getContextPath()%>/css/common.css" type="text/css" />
		<script type='text/javascript' src='../js/sorttable.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/apartmentViewAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/buildingsMgrAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/dormitoryViewAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/engine.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/util.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/jquery-latest.pack.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/noConflictOfJquery.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/forCSS.js'></script>
		<script type="text/javascript">
			  //============全局变量===========
			        var gaptInfo=""; //这个gaptInfo是全局变量 
			        var apartamount; //定义查询出的公寓总条数
			        var totalcount = -1; //查询到的总行数
			        var page;   //dwr分页的page对象，注意在页面中是一个Json对象
			        var dormCache ={};
			        var apartmentid;
			        var dormfloorno;
			        var dormitoryconcretefunc;  //和dormitoryconcretefunc表对应，二维数组
					var lodgeTypeInEditing;//存储正在被编辑的宿舍的住宿房型的原型值
			//=================页面初始化============
			        
			 $j(function(){
			 	getAllDormitoryConcreteFunction();
			 });
			        
			        
			 //===============自定义函数==========
				//查询宿舍所有具体功能名称
				function getAllDormitoryConcreteFunction(){
					
					dormitoryViewAction.getAllDormitoryConcreteFunction(function (funcList){
						//dormitoryconcretefuncid = funcList;
						dormitoryconcretefunc = funcList;
						for(var i=0;i<funcList.length;i++){
							$j("#dormitoryconcretefuncid").append("<option dormfuncid='"+funcList[i][3]+"' value='"+funcList[i][0]+"'>"+funcList[i][1]+"</option>");
						}
						
					});
				}
				//具体功能下拉框变动的时候，住宿或非住宿下拉框联动
				function dormitoryconcretefuncidOnChange(obj){
					var opt = obj.options[obj.options.selectedIndex];
					var dormfuncid = opt.getAttribute("dormfuncid");
					var dormitoryfunction = $j("#dormitoryfunction")[0];
					for(var a=0;a<dormitoryfunction.options.length;a++){
						if(dormitoryfunction.options[a].value ==  dormfuncid){
							dormitoryfunction.options[a].selected = "selected";
						}
					}
					//如果选了非住宿，将住宿房型置0,否则为原始值
					if(dormfuncid == 0){
						$('dormitorylodgetype').value = 0;
					}else{
						$('dormitorylodgetype').value = lodgeTypeInEditing;
					}
				}
				//住宿或非住宿下拉框变动的时候，具体功能下拉框联动
				function dormitoryfunctionOnChange(obj){
					var dormfuncid = obj.options[obj.options.selectedIndex].value;
					var dormitoryfunction = $j("#dormitoryconcretefuncid")[0];
					$j("#dormitoryconcretefuncid").empty();
					for(var a=0;a<dormitoryconcretefunc.length;a++){
						if(dormitoryconcretefunc[a][3] ==  dormfuncid){
							$j("#dormitoryconcretefuncid").append("<option dormfuncid='"+dormitoryconcretefunc[a][3]+"' value='"+dormitoryconcretefunc[a][0]+"'>"+dormitoryconcretefunc[a][1]+"</option>");
						}
					}
					//如果选了非住宿，将住宿房型置0,否则为原始值
					if(dormfuncid == 0){
						$('dormitorylodgetype').value = 0;
					}else{
						$('dormitorylodgetype').value = lodgeTypeInEditing;
					}
					
				}
				//住宿房型值的判断，当房间类型为住宿的时候，住宿房型不可为0或负值
				function dormitorylodgetypeOnChange(obj){
					if($j("#dormitoryfunction").val() == 1){
						if(obj.value <= 0){
							alert("房间类型为住宿的时，住宿类型不可为0或负值");
						}
					}
				}
				function setApartList() {
					 apartmentViewAction.getAllApartInfo(mycallback);
				}
				
				var mycallback = function setList(aptValue)//回调函数
				{
						gaptInfo =aptValue;
						apartamount = gaptInfo.length; //给公寓总条数赋值
						var s = $("aptName");
					 	DWRUtil.removeAllOptions(s); 
						s.options[0] = new Option("请选择公寓",'0');
					for ( var i = 1; i < aptValue.length+1; i++) {
						var opt = new Option(aptValue[i-1].apartmentname,aptValue[i-1].apartmentid);
						s.options[i] = opt;
					}
				}
			
				function selectChange(){
						apartmentid = $("aptName").options[$("aptName").selectedIndex].value; //给apartmentid赋值
						//取到了对应apartmentid的楼层数目
						var aprt_floorno;
						var aprt_name;					
						
						for(var m = 0; m<apartamount;m++){
							if(gaptInfo[m].apartmentid ==apartmentid){
							aprt_floorno = gaptInfo[m].apartmentfloornum;
							aprt_name = gaptInfo[m].apartmentname;
							
							break;
							}
						}
						var sf = $("floorName");
						DWRUtil.removeAllOptions(sf);  
						sf.options[0] = new Option("请选择楼层",'0');
						if(aprt_name == "七号楼"){
						for ( var j = 1; j < (parseInt(aprt_floorno)-1); j++) {
							var optt = new Option((j+2)+"层",(j+2));
							sf.options[j] = optt;
						}
						}
						else if(aprt_name == "三号楼"){
						for ( var j = 1; j < (parseInt(aprt_floorno)); j++) {
							var optt = new Option((j+1)+"层",(j+1));
							sf.options[j] = optt;
						}
						}
						else{
						for ( var j = 1; j < (parseInt(aprt_floorno)+1); j++) {
							var optt = new Option(j+"层",j);
							sf.options[j] = optt;
						}
						
						}
				}
						
					
			//查询总行数
			function searchTotalCount(){
				if(apartmentid==undefined||apartmentid==0){
					alert("请选择公寓！");
					return;
				}
				dormfloorno = $("floorName").options[$("floorName").selectedIndex].value;//给dormfloorno赋值
				if(dormfloorno==undefined||dormfloorno==0){
					alert("请选择公寓楼层！");
					return;
				}
				DWRUtil.removeAllRows('ttb', { filter:function(tr) { //清空上次查询结果
		     		return (tr.id != "pattern");
			    }});
				buildingsMgrAction.getApartmentDormTotalRows(apartmentid,dormfloorno,tcbck); //查询宿舍
			}
			
			var tcbck = function tc_bck(bckcount){
			totalcount = bckcount; // 给总行数赋值
			buildingsMgrAction.ListAllDormListPerPageByApartmentIDAndFloorNo(1,totalcount,apartmentid,dormfloorno,renderdormcallback);
			}
			
			var renderdormcallback  = function renderdormcb(dormlst){
				if(dormlst==null){
				alert("查询公寓宿舍列表为空!");
				return false;
				}
				if($('dorminfodiv').style.visibility == 'visible'){
					$('dorminfodiv').style.visibility = 'hidden';
				}
				if($('pageTable').style.visibility == 'hidden'){
				$('pageTable').style.visibility = 'visible';
				}
				if($('MyTable').style.visibility == 'hidden'){
				$('MyTable').style.visibility = 'visible';
				}
				DWRUtil.removeAllRows('ttb', { filter:function(tr) {
				      return (tr.id != "pattern");
					    }});
				var dom,id;
				 for (var i =0; i < dormlst.length ; i++) {
					  dom = dormlst[i];
					  id = dom.dormitoryid;
					  //alert(id);
					  dwr.util.cloneNode("pattern", { idSuffix:id });
				      dwr.util.setValue("tableid"+id, dom.dormitoryid);//隐藏项
				      dwr.util.setValue("tablename"+id, dom.dormitoryname);
				      dwr.util.setValue("tabledisplayname"+id, dom.dormitorydisplayname);
				      if(dom.dormitorysex==1){
				      dwr.util.setValue("tablesex"+id,'男');
				      }else{
				      dwr.util.setValue("tablesex"+id,'女');
				      }
				      dwr.util.setValue("tablelodgetype"+id,dom.dormitorylodgetype);
				      dwr.util.setValue("tablebuildingtype"+id,dom.dormitorybuildingtype);
				      dwr.util.setValue("tabletelephone"+id,dom.dormitorytelephone);
				      dwr.util.setValue("tableroomfee"+id,dom.dormitoryfee); //隐藏项
				      dwr.util.setValue("tablefunction"+id,dom.dormitoryfunction);//隐藏项
				      dwr.util.setValue("tableapartmentid"+id,dom.apartmentid); //隐藏项
				      dwr.util.setValue("tableconcretefunc"+id,dom.dormitoryconcretefuncid); //隐藏项
				      dwr.util.setValue("tabledirection"+id,dom.dormitorydirection); //隐藏项
				      dwr.util.setValue("tablelevel"+id,dom.dormitorylevel); //隐藏项
				      dwr.util.setValue("tablesquare"+id,dom.dormitorysquare); //隐藏项
				      dwr.util.setValue("tablemisc"+id,dom.dormitorymisc); //隐藏项
				      dwr.util.setValue("cbx" + id, dom.dormitoryid);
				 	  $("pattern" + id).style.display= "";
				 	  dormCache[id] = dom;
				   }
				   	setPageInfo();
				   	tbCSS();
			  }
			
			
		function setPageInfo(){
        $('pageTd').innerHTML = '';
        buildingsMgrAction.getPage({     
            callback:function(data){
                page = data;
                if(page.totalPage != 0){
                    var pageSpan = document.createElement('span');
                    pageSpan.innerHTML = '共 '+totalcount+' 条 '+'   第 '+page.currentPage+' 页    共'+page.totalPage+' 页 ';
                    $('pageTd').appendChild(pageSpan);
                    var aFirst = document.createElement('a');
                    aFirst.setAttribute('href','javascript:getFirstData()');
                    aFirst.innerHTML = '首页';
                    $('pageTd').appendChild(aFirst);
                    var aPrevious = document.createElement('a');
                    aPrevious.setAttribute('href','javascript:getPreviousData()');
                    aPrevious.innerHTML = '上一页 ';
                    $('pageTd').appendChild(aPrevious);
                    var aNext = document.createElement('a');
                    aNext.setAttribute('href','javascript:getNextData()');
                    aNext.innerHTML = '下一页 ';
                    $('pageTd').appendChild(aNext);
                    var aLast = document.createElement('a');
                    aLast.setAttribute('href','javascript:getLastData()');
                    aLast.innerHTML = '尾页';
                    $('pageTd').appendChild(aLast);   
                }              
            }
        });
        $('loading').style.display = 'none';
    } 

		function getFirstData(){
			page.currentPage = 1;
			getDataPerPage();
		}
		
		 function getPreviousData(){
        if(page.currentPage>1){
            page.currentPage--;
        }
        getDataPerPage();
    }
    
   		 function getNextData(){
        if(page.currentPage<page.totalPage){
            page.currentPage++;
        }
        getDataPerPage();
   	 }
		
		function getLastData(){
			page.currentPage = page.totalPage;
			getDataPerPage();
		}
		
		function getDataPerPage(){
			$('loading').style.display = 'block';
			buildingsMgrAction.ListAllDormListPerPageByApartmentIDAndFloorNo(page.currentPage,totalcount,apartmentid,dormfloorno,renderdormcallback)
		}	


		function editClicked(eleid) {
			if($('dorminfodiv').style.visibility == 'hidden'){
			$('dorminfodiv').style.visibility = 'visible';
			}
			var dm = dormCache[eleid.substring(4)]; 
			//dwr.util.setValues(dm);   
			dwr.util.setValue("dormitoryid",dm.dormitoryid);
			dwr.util.setValue("apartmentid",dm.apartmentid);
			dwr.util.setValue("allocationid",dm.allocationid);
			dwr.util.setValue("dormitorystatusid",dm.dormitorystatusid);
			dwr.util.setValue("dormitorysort",dm.dormitorysort);
			dwr.util.setValue("dormitoryind",dm.dormitoryind);
			dwr.util.setValue("dormitoryinstudentnum",dm.dormitoryinstudentnum);
			dwr.util.setValue("dormitoryname",dm.dormitoryname);
			dwr.util.setValue("dormitorydisplayname",dm.dormitorydisplayname);
			dwr.util.setValue("dormitorysex",dm.dormitorysex);
			dwr.util.setValue("dormitorylodgetype",dm.dormitorylodgetype);
			lodgeTypeInEditing = dm.dormitorylodgetype;
			dwr.util.setValue("dormitorybuildingtype",dm.dormitorybuildingtype);
			dwr.util.setValue("dormitorytelephone",dm.dormitorytelephone);
			dwr.util.setValue("dormitoryfee",dm.dormitoryfee);
			dwr.util.setValue("dormitoryfloorno",dm.dormitoryfloorno);
			dwr.util.setValue("dormitoryfunction",dm.dormitoryfunction);
			dwr.util.setValue("dormitoryconcretefuncid",dm.dormitoryconcretefuncid); //隐藏项
			dwr.util.setValue("dormitorydirection",dm.dormitorydirection); //隐藏项
			dwr.util.setValue("dormitorylevel",dm.dormitorylevel); //隐藏项
			dwr.util.setValue("dormitorysquare",dm.dormitorysquare); //隐藏项
			dwr.util.setValue("dormitorymisc",dm.dormitorymisc); //隐藏项
				
		}
		
		//删除宿舍
		function delClicked(eleid){
		//得到的eleid为"mydelete{id}"
		var dorm_id = eleid.substring(8);
		//alert(dorm_id);
		 buildingsMgrAction.delelteDormitoryByID(dorm_id,delcb)
		}
	
		var delcb = function del_cb(stas){
			alert(stas);
			searchTotalCount();
		}

	function writeDorm() {
		var dmty = {
			dormitoryid:null,
			allocationid:null,
			apartmentid:null,
			dormitorystatusid:null,
			dormitoryname:null,
			dormitorydisplayname:null,
			dormitorysex:null,
			dormitorylodgetype:null,
			dormitorybuildingtype:null,
			dormitorytelephone:null,
			dormitoryfee:null,
			dormitoryfunction:null,
			dormitoryfloorno:null,
			dormitorysort:null,
			dormitoryinstudentnum:null,
			dormitoryind:null,
			dormitoryconcretefuncid:null,
			dormitorymisc:null,
			dormitorydirection:null,
			dormitorylevel:null,
			dormitorysquare:null
		};
		dwr.util.getValues(dmty);
		if(dmty.dormitorydisplayname==""){
				alert("请填写宿舍显示名");
				return;
		}
		if(dmty.dormitorysex==""){
				alert("请填写宿舍性别");
				return;
		}
		buildingsMgrAction.updateDormitory(dmty,writecallback);
	}
	var writecallback = function wcallback(status){
	 alert(status);
	 searchTotalCount();
	}
		 
	function clearDorm() { //id没有更新,相当于清除充填
	  //alert('clear');
	  dwr.util.setValues({dormitoryname:null, dormitorydisplayname:null, dormitorysex:null,dormitoryroomtype:null,dormitorytelephone:null,dormitoryfee:null,dormitoryfunction:null });
	}
	//判断房间面积
	 function checkSquare(obj) 
	{ 
		//判断是否为整数或小数或默认的-1
		var patrn=/^[0-9]{1}([0-9]{0,4}|.[0-9]{1,6})|-1$/; 
		if (!patrn.exec(obj.value)){
			obj.value = "输入错误";
			obj.style.color = "red";
		}
		else return true ;
	}
	
			</script>
	</head>
	<body onload="javascript:setApartList()">
		<div id="page_container">
			<%@include file="mgrbanner.jsp"%>
			<h2>
				宿舍列表
			</h2>
			<div>
				<table align="center">
					<tr>
						<td>
							<select class="common_select" name="aptName" id="aptName" style="width: 120"
								onchange="javascript:selectChange();"></select>
						</td>
						<td>
							<select class="common_select" name="floorName" id="floorName" style="width: 120">
							</select>
						</td>
						<td>
							<input class="common_button" type="button" value="提交"
								onclick="javascript:searchTotalCount();" />
						</td>
					</tr>
				</table>
			</div>
			<!-- 隐藏的项目开始 -->
			<div align="center" id="tableid" style="display: none"></div>
			<div align="center" id="tableroomfee" style="display: none"></div>
			<div align="center" id="tablefunction" style="display: none"></div>
			<div align="center" id="tableapartmentid" style="display: none"></div>
			<!-- 隐藏的项目结束 -->
			<table id="MyTable" style="visibility: hidden;" class="table_common sortable table_list_style" width="600px;">
				<thead>
					<tr>
						<th width="12%">
							<div>
								公寓名
							</div>
						</th>
						<th width="25%">
							<div>
								显示名
							</div>
						</th>
						<th width="10%">
							<div>
								住宿性别
							</div>
						</th>
						<th width="10%">
							<div>
								住宿房型
							</div>
						</th>
						<th width="10%">
							<div>
								建筑房型
							</div>
						</th>
						<th width="14%">
							<div>
								电话
							</div>
						</th>
						<th width="5%">
							<div>
								编辑
							</div>
						</th>
						<th width="5%">
							<div>
								删除
							</div>
						</th>
					</tr>
				</thead>
				<tbody id="ttb">
					<tr id="pattern" style="display: none;">
						<!-- 
							<td height="18">
								<div align="center">
									<input type="checkbox" id="cbx" name="cbx" value="checkbox" />
								</div>
							</td>
							 -->
						<td>
							<div id="tablename">
						</td>
						<td>
							<div id="tabledisplayname">
						</td>
						<td>
							<div id="tablesex">
						</td>
						<td>
							<div id="tablelodgetype">
						</td>
						<td>
							<div id="tablebuildingtype">
						</td>
						<td>
							<div id="tabletelephone">
						</td>
						<td>
							<div >
								<a href="#" id="edit" onclick="javascript:editClicked(this.id)"><img
										src="../images/037.gif" width="9" height="9" />
								</a>
							</div>
						</td>
						<td>
							<div >
								<a href="#" id="mydelelt"
									onclick="javascript:if(confirm('确认删除！'))delClicked(this.id)"><img
										src="../images/010.gif" width="9" height="9" /> </a>
							</div>
						</td>
					</tr>
				</tbody>
			</table>
			<table id="pageTable" 
				style="visibility: hidden; margin-top: 30px; font: 12px;"
				class="navigator">
				<tr>
					<td id="pageTd">
					</td>
				</tr>
			</table>
			<div id="loading"
				style="display: none; margin-top: 20px; text-align: center; color: red; font: 13px;">
				Loading...
			</div>
			<!-- dormitory显示div -->
			<div id="dorminfodiv" style="visibility: hidden;">
				<table>
					<tr>
						<td>
							宿舍名:
						</td>
						<td>
							<input id="dormitoryid" type="hidden" />
							<input id="apartmentid" type="hidden" />
							<input id="allocationid" type="hidden" />
							<input id="apartmentid" type="hidden" />
							<input id="dormitorystatusid" type="hidden" />
							<input id="dormitoryfloorno" type="hidden" />
							<input id="dormitorysort" type="hidden" />
							<input id="dormitoryind" type="hidden" />
							<input id="dormitoryinstudentnum" type="hidden" />
							<input id="dormitoryname" type="text" size="10" readonly />
						</td>
					</tr>
					<tr>
						<td>
							宿舍显示名：
						</td>
						<td>
							<input id="dormitorydisplayname" type="text" size="20" readOnly/>
						</td>
					</tr>
					<tr>
						<td>
							住宿性别：
						</td>
						<td>
							<select name="dormitorysex" id="dormitorysex">
								<option value="1">
									男
								</option>
								<option value="0">
									女
								</option>
							</select>
						</td>
					</tr>
					<tr>
						<td>
							住宿房型：
						</td>
						<td>
							<input type="text" id="dormitorylodgetype" size="40" onchange="dormitorylodgetypeOnChange(this)" />
						</td>
					</tr>
					<tr>
						<td>
							建筑房型：
						</td>
						<td>
							<input type="text" id="dormitorybuildingtype" size="40" />
						</td>
					</tr>
					<tr>
						<td>
							宿舍电话：
						</td>
						<td>
							<input type="text" id="dormitorytelephone" size="40" />
						</td>
					</tr>

					<tr>
						<td>
							住宿费用：
						</td>
						<td>
							<input type="text" id="dormitoryfee" size="40" />
						</td>
					</tr>
					<tr>
						<td>
							房间类型：
						</td>
						<td>
							<select name="dormitoryfunction" id="dormitoryfunction" onchange="dormitoryfunctionOnChange(this)">
								<option value="1">
									住宿
								</option>
								<option value="0">
									非住宿
								</option>
							</select>
						</td>
					</tr>
					<tr>
						<td>
							具体功能：
						</td>
						<td>
							<select name="dormitoryconcretefuncid" id="dormitoryconcretefuncid" onchange="dormitoryconcretefuncidOnChange(this)">
								
							</select>
						</td>
					</tr>
					<tr>
						<td>
							朝向
						</td>
						<td>
							<input type="text" id="dormitorydirection" size="40" />
						</td>
					</tr>
					<tr>
						<td>
							档次
						</td>
						<td>
							<input type="text" id="dormitorylevel" size="40" />
						</td>
					</tr>
					<tr>
					<tr>
						<td>
							面积
						</td>
						<td>
							<input type="text" id="dormitorysquare" size="40" onblur="checkSquare(this)" onfocus="this.style.color='black'"/>
						</td>
					</tr>
					<tr>
						<td>
							备注
						</td>
						<td>
							<input type="text" id="dormitorymisc" size="40" />
						</td>
					</tr>
					
					
					
					<tr>
						<td colspan="2" align="right">
							<input class="common_button" type="button" value="保存" onclick="writeDorm()" />
							<!-- 
							<input class="common_button" type="button" value="清除" onclick="clearDorm()" />
							 -->
						</td>
					</tr>
				</table>
			</div>
		</div>
	</body>
</html>
